package cn.itsource.rpms.web.controller;


import cn.itsource.rpms.domain.Part;
import cn.itsource.rpms.query.PartQuery;
import cn.itsource.rpms.service.IPartService;
import cn.itsource.rpms.util.AjaxResult;
import cn.itsource.rpms.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/part")
@Api(tags = "配件接口类")
@CrossOrigin
public class PartController {

    @Autowired
    private IPartService partService;


    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
         @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @CrossOrigin

    public List<Part> queryAll(){
        System.out.println("patch请求");
        return partService.queryAll();
    }

    // /department/1
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)

    @ResponseBody
    public Part queryOne(@PathVariable Long id){

        return  partService.queryOne(id);
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    public AjaxResult save(@RequestBody Part part){
        try {
            System.out.println("接收的part对象:"+part);
            partService.add(part);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);
            partService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Part part){
        try {
            System.out.println("修的对象:"+part);
            System.out.println(part.getId());
            partService.update(part);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");

        }
        return new AjaxResult();
    }

    //批量删除
    @RequestMapping(value = "/batchDelete",method = RequestMethod.PATCH)
    @ResponseBody
    public AjaxResult batchDelete(Long[] ids){
        try {
            List<Long> idList = Arrays.asList(ids);

            partService.batchDelete(idList);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");

        }
        return new AjaxResult();
    }
    //分页
    //分页查询方法
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @CrossOrigin
    public PageResult<Part> queryPage(@RequestBody PartQuery partQuery){
        return partService.queryPage(partQuery);
    }
}
